﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PRE
{
    public partial class frmGM : Form
    {
        DAL.EntitiesDAL.ban_DAL _dal = new DAL.EntitiesDAL.ban_DAL();
        DAL.EntitiesDAL.chitietphieuDAL _dll = new DAL.EntitiesDAL.chitietphieuDAL();
        BLL.EntitiesBLL.phieuBLL _bll = new BLL.EntitiesBLL.phieuBLL();
        DAL.EntitiesDAL.phieu_DAL _dall = new DAL.EntitiesDAL.phieu_DAL();
        public frmGM()
        {
            InitializeComponent();
        }

        protected DataTable _dtb = new DataTable("Tmp");
        private void frmGM_Load(object sender, EventArgs e)
        {
            txtTongtien.Text = "0";
            DataTable _DDL = _dll.load_khuvuc();
            cbKV.Properties.DataSource = _DDL;
            //load loai mon
            DataTable _loai = _dal.Select_load();
            cbLoaimon.Properties.DataSource = _loai;
            //load ban chua tao phieu
            DataTable _ban = _dal.BAN();
            cbBan.Properties.DataSource = _ban;

            if (txtTongtien.Text == null)
            {
                txtTongtien.Text = "0";
            }

         //   loadban();
        }

        void load_tt()
        {
            lvBan.Items.Clear();
            _dtb = _dal.Select_KV(cbKV.EditValue + "");
            DataTable dlll = _dal.load();

            foreach (DataRow drw in _dtb.Rows)
            {
                //luu.Text = drw["maban"].ToString();
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;

                foreach (DataRow row in dlll.Rows)
                {

                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                    }
                    //else
                    //{
                    //    lvwitem.ImageIndex = 0;
                    //    hinh = 0;

                    //}
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                lvBan.Items.Add(lvwitem);
            }
        }


        void loadban()
        {
            //lvwTDofBan.DataBindings.Clear();
            //lvBan.DataBindings.Clear();
            lvBan.Items.Clear();
            _dtb = _dal.Select_KV(cbKV.EditValue + "");
            DataTable dlll = _dal.load();

            foreach (DataRow drw in _dtb.Rows)
            {
                //luu.Text = drw["maban"].ToString();
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;
                foreach (DataRow row in dlll.Rows)
                {
                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                        //luu.Text = row["maban"].ToString();
                    }
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                lvBan.Items.Add(lvwitem);
            }
        }

        private void cbKV_EditValueChanged(object sender, EventArgs e)
        {
            
            lvBan.Items.Clear();
            loadban();
            
        }

        private void lvBan_SelectedIndexChanged(object sender, EventArgs e)
        {
             _dtb = _dal.Select_KV(cbKV.EditValue + "");
            DataTable dlll = _dal.load();

            foreach (DataRow drw in _dtb.Rows)
            {

                
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;
                foreach (DataRow row in dlll.Rows)
                {
                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                        //luu.Text = row["maban"].ToString();
                    }
                    luu.Text = lvwitem.Tag.ToString();
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                //lvBan.Items.Add(lvwitem);
                //luu.Text = drw["maban"].ToString();
            }
        }

        private void labelControl2_Click(object sender, EventArgs e)
        {

        }

        private void cbLoaimon_EditValueChanged(object sender, EventArgs e)
        {
            _dtb = _dal.thucdon_loaimon(cbLoaimon.EditValue + "");
            dsThucdon.DataSource = _dtb;
        }

        private void cmdTaoban_Click(object sender, EventArgs e)
        {
           
            var o = new DAL.Entities.phieu()
            {
                MAPHIEU = txtSTT.Text,
                TINHTIEN = txtTongtien.Text,
                NGAYTAOPHIEU = Convert.ToDateTime(dtNgay.EditValue),
                MABAN = cbBan.EditValue + ""
            };
            
            var oki = _dall.Insert(o);
            if (oki)
            {
                MessageBox.Show("Đã tạo bàn thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
               
            }
            else
            {
                MessageBox.Show("Lỗi! Tạo bàn thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
            }
            load_tt();
            //frmGM_Load(sender, e);
        }

        private void cbBan_EditValueChanged(object sender, EventArgs e)
        {
            
            
            frmGM_Load(sender, e);
            DataTable _ma = _dall.Select_max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
                    txtSTT.Text = ma;
                }
        }

        private void dsThucdon_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void cbBan_EditValueChanged_1(object sender, EventArgs e)
        {
            frmGM_Load(sender, e);
            DataTable _ma = _dall.Select_max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
                    txtSTT.Text = ma;
                }
        }

        private void cmdTaoban_Click_1(object sender, EventArgs e)
        {

        }

      
    }
}
